package net.wofly.sale.signin.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.web.BaseController;
import net.wofly.sale.signin.domain.SignIn;
import net.wofly.sale.signin.service.ISignInService;
import net.wofly.sale.signin.validator.SignInValidator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author siqingwei
 */
@RestController
@RequestMapping(value = "/api/signIn")
@Api(description = "签到接口")
public class SignInController extends BaseController {

	@Autowired
	ISignInService iSignInService;

	/**
	 *
	 * @param employeeID
	 * @param fromTime
	 * @param toTime
	 */
	@ApiOperation(value = "获取签到记录", notes = "获取签到记录")
	@ApiImplicitParams({@ApiImplicitParam(name = "employeeID", value = "员工ID", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "fromTime", value = "Long", required = false, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "toTime", value = "员工ID", required = false, dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "Integer", paramType = "query")})
	@RequestMapping(value = "/getSignIn", method = {RequestMethod.GET})
	ReturnBean<PageWrapper<SignIn>> getSignIn(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
											  @RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize,
											  String employeeID, Long fromTime, Long toTime){
		ReturnBean<PageWrapper<SignIn>> returnBean;
		String errorMessage = SignInValidator.validateGetSignInParams(employeeID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iSignInService.getSignIn(pageNum, pageSize, employeeID, fromTime, toTime);
		}
		return returnBean;
	}

	/**
	 * 签到
	 * <ul>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param signIn
	 */
	@ApiOperation(value = "签到", notes = "签到")
	@ApiImplicitParam(name = "signIn", value = "签到实体", required = true, dataType = "SignIn")
	@RequestMapping(value = "/signIn", method = {RequestMethod.POST})
	ReturnBean<String> signIn(@RequestBody SignIn signIn){
		ReturnBean<String> returnBean;
		String errorMessage = SignInValidator.validateSignInParams(signIn);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iSignInService.signIn(signIn);
		}
		return returnBean;
	}

	/**
	 * 上传签到图片
	 * <ul>
	 * 	<li>返回图片存放路径</li>
	 * 	<li>根据上传时间计算图片存放位置</li>
	 * </ul>
	 *
	 * @param file
	 */
	@ApiOperation(value = "上传签到图片", notes = "上传签到图片")
	@ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file", paramType = "form")
	@RequestMapping(value = "/uploadPic", method = {RequestMethod.POST})
	ReturnBean<String> uploadPic(@RequestParam("file") MultipartFile file, HttpServletRequest request){
		ReturnBean<String> returnBean;
		String errorMessage = SignInValidator.validateUploadPicParams(file);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			String path = request.getSession().getServletContext().getRealPath("/");
			returnBean = iSignInService.uploadPic(file, path);
		}
		return returnBean;
	}

}
